<?php
/**
 * @title 3076. 数组中的最短非公共子字符串
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }


class Solution {

    /**
     * @param String[] $arr
     * @return String[]
     */
    function shortestSubstrings($arr) {
        /**
         * @since 2024-03-11 记录字符串第一次出现的arr-k
         */
        $indexArray = [];

        /**
         * @since 2024-03-11 遍历字符arr，不是 arr 中其他任何字符串的子字符串
         */
        $subArray = [];
        foreach ($arr as $k=>$s){
            $hash1 = $hash2 = [];
            $sLength = strlen($s);
            for($i=0; $i<$sLength; $i++){
                for($j=$i+1; $j<=$sLength; $j++){
                    $subString = substr($s, $i, $j-$i);
                    if(!isset($hash1[$subString])){
                        $hash1[$subString] = $subString;

                        /**
                         * @since 2024-03-11 是否第一次出现
                         */
                        if(isset($indexArray[$subString])){
                            if($indexArray[$subString] !== false){
                                $index = $indexArray[$subString];
                                unset($subArray[$index][$subString]);
                                $indexArray[$subString] = false;
                            } else {}
                        } else {
                            $hash2[$subString] = $subString;
                            $indexArray[$subString] = $k;
                        }
                    } else {}
                }
            }
            $subArray[$k] = $hash2;
        }

        /**
         * @since 2024-03-11 请你返回数组 answer
         */
        $answerArray = [];
        foreach ($subArray as $sArray){
            $minLength = PHP_INT_MAX;
            $minSubString = "";

            foreach ($sArray as $s){
                $sLength = strlen($s);
                if($sLength < $minLength){
                    $minLength = $sLength;
                    $minSubString = $s;
                } elseif($sLength == $minLength AND $s < $minSubString){
                    $minSubString = $s;
                } else {}
            }
            $answerArray[] = $minSubString;
        }

        /**
         * @return String[]
         */
        return $answerArray;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202403/2024.03.11_4.php
 */
$datas = [
    [["cab","ad","bad","c"]], // ab, empty, ba, emtpy
    // [["abc","bcd","abcd"]], // empty, emtpy, abcd
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->shortestSubstrings(...$data);
    echo $xhprof->DumpArgs([$result]);
    var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
